﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:Etype
    /// </summary>
    public partial class Etype
    {
        public Etype()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("EtypeFID", "Etype");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int EtypeFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from Etype");
            strSql.Append(" where EtypeFID=@EtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@EtypeFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = EtypeFID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.Etype model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into Etype(");
            strSql.Append("Etypeid,Parid,Leveal,Soncount,Esonnum,Efullname,Ename,Eusercode,Tel,Address,Ecomment,ENamePY,Birthday,Email,IsStop,DeptID)");
            strSql.Append(" values (");
            strSql.Append("@Etypeid,@Parid,@Leveal,@Soncount,@Esonnum,@Efullname,@Ename,@Eusercode,@Tel,@Address,@Ecomment,@ENamePY,@Birthday,@Email,@IsStop,@DeptID)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@Etypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Parid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@Soncount", SqlDbType.Int, 4),
                    new SqlParameter("@Esonnum", SqlDbType.Int, 4),
                    new SqlParameter("@Efullname", SqlDbType.VarChar, 200),
                    new SqlParameter("@Ename", SqlDbType.VarChar, 50),
                    new SqlParameter("@Eusercode", SqlDbType.VarChar, 100),
                    new SqlParameter("@Tel", SqlDbType.VarChar, 30),
                    new SqlParameter("@Address", SqlDbType.VarChar, 256),
                    new SqlParameter("@Ecomment", SqlDbType.VarChar, 256),
                    new SqlParameter("@ENamePY", SqlDbType.VarChar, 66),
                    new SqlParameter("@Birthday", SqlDbType.DateTime),
                    new SqlParameter("@Email", SqlDbType.VarChar, 50),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@DeptID", SqlDbType.VarChar, 50) };
            parameters[0].Value = model.Etypeid;
            parameters[1].Value = model.Parid;
            parameters[2].Value = model.Leveal;
            parameters[3].Value = model.Soncount;
            parameters[4].Value = model.Esonnum;
            parameters[5].Value = model.Efullname;
            parameters[6].Value = model.Ename;
            parameters[7].Value = model.Eusercode;
            parameters[8].Value = model.Tel;
            parameters[9].Value = model.Address;
            parameters[10].Value = model.Ecomment;
            parameters[11].Value = model.ENamePY;
            parameters[12].Value = model.Birthday;
            parameters[13].Value = model.Email;
            parameters[14].Value = model.IsStop;
            parameters[15].Value = model.DeptID;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.Etype model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update Etype set ");
            strSql.Append("Etypeid=@Etypeid,");
            strSql.Append("Parid=@Parid,");
            strSql.Append("Leveal=@Leveal,");
            strSql.Append("Soncount=@Soncount,");
            strSql.Append("Esonnum=@Esonnum,");
            strSql.Append("Efullname=@Efullname,");
            strSql.Append("Ename=@Ename,");
            strSql.Append("Eusercode=@Eusercode,");
            strSql.Append("Tel=@Tel,");
            strSql.Append("Address=@Address,");
            strSql.Append("Ecomment=@Ecomment,");
            strSql.Append("ENamePY=@ENamePY,");
            strSql.Append("Birthday=@Birthday,");
            strSql.Append("Email=@Email,");
            strSql.Append("IsStop=@IsStop,");
            strSql.Append("DeptID=@DeptID");
            strSql.Append(" where EtypeFID=@EtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@Etypeid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Parid", SqlDbType.VarChar, 25),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@Soncount", SqlDbType.Int, 4),
                    new SqlParameter("@Esonnum", SqlDbType.Int, 4),
                    new SqlParameter("@Efullname", SqlDbType.VarChar, 200),
                    new SqlParameter("@Ename", SqlDbType.VarChar, 50),
                    new SqlParameter("@Eusercode", SqlDbType.VarChar, 100),
                    new SqlParameter("@Tel", SqlDbType.VarChar, 30),
                    new SqlParameter("@Address", SqlDbType.VarChar, 256),
                    new SqlParameter("@Ecomment", SqlDbType.VarChar, 256),
                    new SqlParameter("@ENamePY", SqlDbType.VarChar, 66),
                    new SqlParameter("@Birthday", SqlDbType.DateTime),
                    new SqlParameter("@Email", SqlDbType.VarChar, 50),
                    new SqlParameter("@IsStop", SqlDbType.Int, 4),
                    new SqlParameter("@DeptID", SqlDbType.VarChar, 50),
                    new SqlParameter("@EtypeFID", SqlDbType.Int, 4) };
            parameters[0].Value = model.Etypeid;
            parameters[1].Value = model.Parid;
            parameters[2].Value = model.Leveal;
            parameters[3].Value = model.Soncount;
            parameters[4].Value = model.Esonnum;
            parameters[5].Value = model.Efullname;
            parameters[6].Value = model.Ename;
            parameters[7].Value = model.Eusercode;
            parameters[8].Value = model.Tel;
            parameters[9].Value = model.Address;
            parameters[10].Value = model.Ecomment;
            parameters[11].Value = model.ENamePY;
            parameters[12].Value = model.Birthday;
            parameters[13].Value = model.Email;
            parameters[14].Value = model.IsStop;
            parameters[15].Value = model.DeptID;
            parameters[16].Value = model.EtypeFID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int EtypeFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Etype ");
            strSql.Append(" where EtypeFID=@EtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@EtypeFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = EtypeFID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string EtypeFIDlist)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from Etype ");
            strSql.Append(" where EtypeFID in (" + EtypeFIDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Etype GetModel(int EtypeFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 EtypeFID,Etypeid,Parid,Leveal,Soncount,Esonnum,Efullname,Ename,Eusercode,Tel,Address,Ecomment,ENamePY,Birthday,Email,IsStop,DeptID from Etype ");
            strSql.Append(" where EtypeFID=@EtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@EtypeFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = EtypeFID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.Etype DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.Etype();
            if (row != null)
            {
                if (row["EtypeFID"] != null && row["EtypeFID"].ToString() != string.Empty)
                {
                    model.EtypeFID = int.Parse(row["EtypeFID"].ToString());
                }
                if (row["Etypeid"] != null)
                {
                    model.Etypeid = row["Etypeid"].ToString();
                }
                if (row["Parid"] != null)
                {
                    model.Parid = row["Parid"].ToString();
                }
                if (row["Leveal"] != null && row["Leveal"].ToString() != string.Empty)
                {
                    model.Leveal = int.Parse(row["Leveal"].ToString());
                }
                if (row["Soncount"] != null && row["Soncount"].ToString() != string.Empty)
                {
                    model.Soncount = int.Parse(row["Soncount"].ToString());
                }
                if (row["Esonnum"] != null && row["Esonnum"].ToString() != string.Empty)
                {
                    model.Esonnum = int.Parse(row["Esonnum"].ToString());
                }
                if (row["Efullname"] != null)
                {
                    model.Efullname = row["Efullname"].ToString();
                }
                if (row["Ename"] != null)
                {
                    model.Ename = row["Ename"].ToString();
                }
                if (row["Eusercode"] != null)
                {
                    model.Eusercode = row["Eusercode"].ToString();
                }
                if (row["Tel"] != null)
                {
                    model.Tel = row["Tel"].ToString();
                }
                if (row["Address"] != null)
                {
                    model.Address = row["Address"].ToString();
                }
                if (row["Ecomment"] != null)
                {
                    model.Ecomment = row["Ecomment"].ToString();
                }
                if (row["ENamePY"] != null)
                {
                    model.ENamePY = row["ENamePY"].ToString();
                }
                if (row["Birthday"] != null && row["Birthday"].ToString() != string.Empty)
                {
                    model.Birthday = DateTime.Parse(row["Birthday"].ToString());
                }
                if (row["Email"] != null)
                {
                    model.Email = row["Email"].ToString();
                }
                if (row["IsStop"] != null && row["IsStop"].ToString() != string.Empty)
                {
                    model.IsStop = int.Parse(row["IsStop"].ToString());
                }
                if (row["DeptID"] != null)
                {
                    model.DeptID = row["DeptID"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select EtypeFID,Etypeid,Parid,Leveal,Soncount,Esonnum,Efullname,Ename,Eusercode,Tel,Address,Ecomment,ENamePY,Birthday,Email,IsStop,DeptID ");
            strSql.Append(" FROM Etype ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" EtypeFID,Etypeid,Parid,Leveal,Soncount,Esonnum,Efullname,Ename,Eusercode,Tel,Address,Ecomment,ENamePY,Birthday,Email,IsStop,DeptID ");
            strSql.Append(" FROM Etype ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Etype ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.EtypeFID desc");
            }
            strSql.Append(")AS Row, T.*  from Etype T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 通过编号获取记录
        /// </summary>
        /// <param name="Eusercode"></param>
        /// <returns></returns>
        public DataTable GetRecordByNo(string Eusercode)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 EtypeFID,Etypeid,Parid,Leveal,Soncount,Esonnum,Efullname,Ename,Eusercode,Tel,Address,Ecomment,ENamePY,Birthday,Email,IsStop,DeptID from Etype ");
            strSql.Append(" where Eusercode=@Eusercode");
            var parameters = new SqlParameter[] { new SqlParameter("@Eusercode", SqlDbType.VarChar, 100)
            };
            parameters[0].Value = Eusercode;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 通过编号获取记录条数
        /// </summary>
        public int GetRecordCountByNo(string Eusercode)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) RecordCount from Etype");
            strSql.Append(" where Eusercode=@Eusercode");
            var parameters = new SqlParameter[] { new SqlParameter("@Eusercode", SqlDbType.VarChar, 100)
            };
            parameters[0].Value = Eusercode;

            var recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(strSql.ToString(), parameters));
            return recordCount;
        }
        /// <summary>
        /// 员工是否删除，或被停用
        /// 返回 0:不存在; 1:存在，未停用; 2:存在，停用
        /// </summary>
        /// <param name="Eusercode"></param>
        /// <returns></returns>
        public int ExistsByNo(string Eusercode)
        {
            var strSql = new StringBuilder();
            strSql.Append("delcare int @ifExists, @ifStop ");
            strSql.Append("set @ifExists=0, @ifStop=0 ");
            strSql.Append("if exists (select 1 from Etype where Eusercode=@Eusercode) set @ifExists=1 ");
            strSql.Append("if @ifExists=1 select @ifStop=IsStop from Etype where Eusercode=@Eusercode ");
            strSql.Append("select @ifExists IfExists, @ifStop IfStop");

            var parameters = new SqlParameter[] { new SqlParameter("@Eusercode", SqlDbType.VarChar, 100)
            };
            parameters[0].Value = Eusercode;

            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                var ifExists = Convert.ToInt32(ds.Tables[0].Rows[0]["IfExists"]);
                var ifStop = Convert.ToInt32(ds.Tables[0].Rows[0]["IfStop"]);

                if (ifExists == 0)
                {
                    return 0;
                }
                else
                {
                    if (ifStop == 1)
                    {
                        return 1;
                    }
                    else
                    {
                        return 2;
                    }
                }
            }
            else
            {
                return -1;
            }
        }
    }
}

